import { AbsFormUtil } from '@abstract/form';
import { Component, OnInit } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { GenderEnum } from '@app/store/dict';
import { IGrid } from '@model/grid';
import { AuthService } from '@service/auth.service';
import { GridService } from '@service/grid.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { GridListEditComponent } from './grid-list-edit/grid-list-edit.component';

@Component({
	selector: 'app-grid-list',
	templateUrl: './grid-list.component.html',
	styleUrls: ['./grid-list.component.less']
})
export class GridListComponent extends AbsFormUtil implements OnInit {
	griderType: string;
	genderEnum = GenderEnum;
	nzLoading = false;
	data = [];
	grider = {
		'1': '网格长',
		'2': '网格员',
		'3': '微网格长'
	}
	constructor(
		private route: ActivatedRoute,
		private gridService: GridService,
		private nzMessageService: NzMessageService,
		private fb: FormBuilder,
		private nzModalService: NzModalService,
		private authService: AuthService

	) {
		super();
	}


	ngOnInit() {
		this.buildForm();
		this.route.params.subscribe(data => {
			this.griderType = data.id;
			this.onSearch(true);
			this.authList = this.authService.getAuth();
		});
		

	}
	buildForm() {
		this.validateForm = this.fb.group({
			contractNumber: [''],
		});
	}

	async onSearch(isPageInit?: boolean) {
		this.nzLoading = true;
		if (isPageInit) {
			this.page.page = 1;
		}
		const formValue = this.validateForm.value;
		formValue.griderType = this.griderType;
		const params = { ...formValue, ...this.page };
		const result = await this.gridService.getGriderList(params);
		this.data = result.data.list;
		this.page.total = result.data.totalCount;
		this.nzLoading = false;

	}


	pageChange() {
		console.log(this.page);
		// this.onSearch();
	}

	edit(item) {

	}

	async del(item) {
		await this.gridService.deleteGrider(item.id);
		this.nzMessageService.success('删除成功');
		this.onSearch(true);
	}

	

	add(item?: IGrid) {
	
		this.nzModalService.create({
			nzTitle:( item ? '编辑'  : '新增') + this.grider[this.griderType],
			nzContent: GridListEditComponent,
			nzWidth: '70%',
			nzStyle:{
				top: '50px' 
			},
			nzComponentParams: {
					id: item?.id,
					griderType: this.griderType
			},
			nzFooter: [
				{
					label: '确定',
					type: 'primary',
					onClick: (componentInstance) => {
						(async () => {
							const resForm = componentInstance.onSubmit();
							if (resForm) {
								resForm.id = componentInstance.id || '';
								resForm.griderType =this.griderType;
								resForm.ownerCommunity = resForm.ownerCommunity[resForm.ownerCommunity.length - 1];
								const result = await this.gridService.saveGrider(resForm);
								if (result.status !== 0) {
									this.nzMessageService.error(result.msg || '保存失败');
									return;
								}
								componentInstance.destroyModal();
								item ? Object.assign(item, resForm) : this.onSearch(true);
								this.nzMessageService.success('保存成功');
							}
						})();
					}
				},
				{
					label: '取消',
					onClick: componentInstance => {
						componentInstance.destroyModal();
					}
				}
			]
		})
	}



}
